﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using QuanLyKhachSanDAO.DTO;

namespace QuanLyKhachSanDAO.DAO
{
    public class LoaiPhongDAO
    {
        
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            LoaiPhongDTO lp = new LoaiPhongDTO();
            lp.MaLoaiPhong = int.Parse(dt.Rows[i]["MaLoaiPhong"].ToString());
            lp.TenLoaiPhong = dt.Rows[i]["TenLoaiPhong"].ToString();
            lp.DonGia = float.Parse(dt.Rows[i]["DonGia"].ToString());
            lp.TinhTrang = bool.Parse(dt.Rows[i]["TinhTrang"].ToString());

            return (object)lp;
        }
        public  LoaiPhongDTO[] LayDanhSachLoaiPhong()
        {
            LoaiPhongDTO[] arr;
            string sql = "select * from LoaiPhong";
            DataProvider ds = new DataProvider();
            DataTable dt = ds.ExecuteQuery(sql);
            int i, n = dt.Rows.Count;
            arr = new LoaiPhongDTO[n+1];
            LoaiPhongDTO lp1 = new LoaiPhongDTO();
            lp1.MaLoaiPhong = -1;
            lp1.TenLoaiPhong = "---Chọn loại phòng---";
            lp1.TinhTrang = true;
            lp1.DonGia = 0;
            arr[0] = lp1;
            for (i = 0; i < n; i++)
            {
                object lp = GetDataFromDataRow(dt, i);
                arr[i+1] = (LoaiPhongDTO)lp;
            }
            return arr;
        }

        public LoaiPhongDTO LoaiPhongTheoMa(int maloaiphong)
        {
            DataTable dt = new DataTable();
            string sql = "select * from LoaiPhong where MaLoaiPhong=" + maloaiphong;
            DataProvider ds = new DataProvider();
            dt = ds.ExecuteQuery(sql);
            LoaiPhongDTO lp = new LoaiPhongDTO();
            lp.MaLoaiPhong = int.Parse(dt.Rows[0]["MaLoaiPhong"].ToString());
            lp.TenLoaiPhong = dt.Rows[0]["TenLoaiPhong"].ToString();
            lp.DonGia = float.Parse(dt.Rows[0]["DonGia"].ToString());
            lp.TinhTrang = bool.Parse(dt.Rows[0]["TinhTrang"].ToString());
            return lp;
        }
    }
}